#include "udpclientform.h"
#include "ui_udpclientform.h"

#include <QNetworkInterface>
#include <QTcpSocket>

UdpClientForm::UdpClientForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::UdpClientForm)
    , m_udp(nullptr)
{
    ui->setupUi(this);

    connect(ui->pushButton_connect, &QPushButton::clicked, this, &UdpClientForm::slot_connect);
    connect(ui->pushButton_disconnect, &QPushButton::clicked, this, &UdpClientForm::slot_disconnect);

    auto localAddress = [=](){

        QList<QHostAddress> list =QNetworkInterface::allAddresses();
        foreach (QHostAddress address, list)
        {
            if(address.protocol() ==QTcpSocket::IPv4Protocol)
            {
                ui->comboBox_local_ip->addItem(address.toString());
            }
        }
    };
    localAddress();
}

UdpClientForm::~UdpClientForm()
{
    delete ui;
}


void UdpClientForm::Send(const stParameter& parameter)
{
    if(this->m_udp->isOpen()){
        m_udp->writeDatagram(parameter.Array, QHostAddress(ui->lineEdit_remote_ip->text()), ui->lineEdit_remote_port->text().toUInt());
    }
}


#include <QNetworkDatagram>
void UdpClientForm::slot_readPendingDatagrams()
{
    while (m_udp->hasPendingDatagrams()) {
        QNetworkDatagram datagram = m_udp->receiveDatagram();
        emit this->SigData(datagram.data());
    }
}

#include <QMessageBox>
void UdpClientForm::slot_connect()
{
    if(nullptr == m_udp){
        m_udp = new QUdpSocket(this);
        bool bl = m_udp->bind(QHostAddress::LocalHost, ui->lineEdit_local_port->text().toUInt());
        if(bl){
            QMessageBox msgBox;
            msgBox.setText("绑定端口连接成功");
            msgBox.setInformativeText("提示");
            msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
            msgBox.exec();
        }else{
            QMessageBox msgBox;
            msgBox.setText("绑定端口连接失败");
            msgBox.setInformativeText("提示");
            msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
            msgBox.exec();
            return;
        }

        connect(m_udp, &QUdpSocket::readyRead,
                this, &UdpClientForm::slot_readPendingDatagrams);
    }else{
        if(m_udp->isValid()){
            QMessageBox msgBox;
            msgBox.setText("已经连接成功, 无需重复连接");
            msgBox.setInformativeText("提示");
            msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
            msgBox.exec();
        }else{
            bool bl = m_udp->bind(QHostAddress::LocalHost, ui->lineEdit_local_port->text().toUInt());
            if(bl){
                QMessageBox msgBox;
                msgBox.setText("绑定端口连接成功");
                msgBox.setInformativeText("提示");
                msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
                msgBox.exec();
            }else{
                QMessageBox msgBox;
                msgBox.setText("绑定端口连接失败");
                msgBox.setInformativeText("提示");
                msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
                msgBox.exec();
                return;
            }
        }

    }
}


void UdpClientForm::slot_disconnect()
{
    if(m_udp && m_udp->isOpen()){
        m_udp->close();
        QMessageBox msgBox;
        msgBox.setText("端口连接断开");
        msgBox.setInformativeText("提示");
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
        msgBox.exec();
    }
}
